layui.use(['layer','upload','layedit','treeSelect','table','form','laytpl','element'], function(){
    var $ = layui.$
        ,layer = layui.layer
        ,admin = layui.admin
        ,table = layui.table
        ,element = layui.element
        ,form = layui.form
        ,laytpl = layui.laytpl;
    var delFlag = null;
    var treeSelect= layui.treeSelect;//树形下拉框
    var upload = layui.upload;
   window.layedit = layui.layedit;

    //获取路径参数
    var request = GetRequest();

    //加载场地信息
    treeSelect.render({
        elem: '#fieldType',
        data: '/field/type/tree/select',
        type: 'get',
        placeholder: '顶级分类',// 占位符
        search: true,// 是否开启搜索功能：true/false，默认false
        click: function(data){
            $("#tree-select").val(data.current.id);
        },
        success: function (data) { // 加载完成后的回调函数
            $("#tree-select").val("0");
        }
    });

    $.get("/process/qrcode/" + request.id,function (data) {
        $("#id").val(data.id);
        $("#productBatchId").val(data.productBatchId);
        $("#processId").val(data.processId);
        $("#name").val(data.name);

        //加载当前环节的记录表
        $.get('/process/' + data.processId,function (process) {
            var tableArr = process.tableType.split(",");
            $.each(tableArr,function (i,ele) {
                if (ele == 'invest') {
                    $("#tableName").append('<option value="'+ ele +'">投入品记录</option>');
                }
                else if (ele == 'check') {
                    $("#tableName").append('<option value="'+ ele +'">检查记录</option>');
                } else if (ele == 'machining') {
                    $("#tableName").append('<option value="'+ ele +'">加工记录</option>');
                } else if (ele == 'transport') {
                    $("#tableName").append('<option value="'+ ele +'">运输记录</option>');
                } else {
                    //查询自定义表记录
                    $.ajax({
                        url:'/table/' + ele,
                        type:'GET',
                        async:false,
                        success:function (data){
                            $("#tableName").append('<option value="'+ ele +'">'+ data.nameZh +'</option>');
                        }
                    });
                    /*$.get('/table/' + ele,function (data) {
                        $("#tableName").append('<option value="'+ ele +'">'+ data.nameZh +'</option>');
                        form.render('select');
                    });*/
                }

            });
            $("#tableName option[value="+ data.tableName +"]").attr("selected","selected");
            form.render('select');
        });

        //加载地块信息
        $.get("/field/list",function (field) {
            $.each(field.data,function (i,ele) {
                if(data.fieldId == ele.id) {
                    $("#fieldId").append('<option selected="selected" value="'+ ele.id +'">'+ ele.name +'</option>');
                } else {
                    $("#fieldId").append('<option value="'+ ele.id +'">'+ ele.name +'</option>');
                }

            });
            form.render('select');
        });

        //加载负责人信息列表
        $.get('/person/list',function (person) {
            $.each(person.data,function (i,ele) {
                if(data.personId == ele.id) {
                    $("#personId").append('<option selected="selected" value="'+ ele.id +'">'+ ele.name +'</option>');
                } else {
                    $("#personId").append('<option value="'+ ele.id +'">'+ ele.name +'</option>');
                }

            });
            form.render('select');
        });
        $("#remarks").val(data.remarks);

        //二维码信息
        $("#requestUrl").val(data.requestUrl);
        $("#qrcode").qrcode(data.requestUrl);

    });

    //修改产品批次信息
    form.on('submit(edit)', function(data){
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        //初始化数据
        $.ajax({
            url: '/process/qrcode/edit',
            data: data.field,
            type: 'POST',
            success: function (data) {
                if(data.code == 200) {
                    top.layer.close(index);
                    top.layer.msg("修改成功！");
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
                } else {
                    top.layer.close(index);
                    top.layer.msg("修改失败！");
                }
            },
            error:function () {
                top.layer.close(index);
                top.layer.msg("修改失败！");
            }
        });
        return false;
    });

    /**
     * 获取路径中的参数
     * @returns {Object}
     */
    function GetRequest() {
        var url = location.search;         //获取url中"?"符后的字串
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            var strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    }

});

